package club.xinghe.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.Reader;

/**
 * mybatis工厂类
 */

public class SqlSessionFactoryUtil {

    private static SqlSessionFactory factory;
    private SqlSessionFactoryUtil(){
        //私有的构造方法 --单例模式
    }
    //静态代码块
    static {
        Reader reader=null;
        try {
            reader= Resources.getResourceAsReader("mybatis.xml");
            factory=new SqlSessionFactoryBuilder().build(reader);

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
    //获取session的方法
    public static SqlSession getSqlSession(){
        return factory.openSession();
    }


}
